const obj = { 0: 10, 1: 20, 2: 30, length: 3 };

obj[Symbol.iterator] = function () {
  const that = this;
  return that;
  return {
    curIndex: 0,
    next() {
      if (this.curIndex < that.length) {
        return { done: false, value: that[this.curIndex++] };
      } else {
        return { done: true };
      }
    }
  };
}
obj.next = function () {
  
}

for (let n of obj) {
  console.log(n);
}

// const useState = function(initVal){
//     return {
//         value: initVal,
//         setValue: function (newVal) {
//             console.log('newVal', newVal);
//         },
//         [Symbol.iterator]: function () {
//             console.log('Symbol.iterator');
//             const that = this;
//             return {
//                 keys: ['value', 'setValue'],
//                 currentVal: 0,
//                 next() {
//                     const curKey = this.keys[this.currentVal];
//                     if(this.currentVal < this.keys.length) {
//                         this.currentVal++;
//                         return {done: false, value: that[curKey]};
//                     } else {
//                         return {done: true};
//                     }
//                 }
//             };
//         }
//     };
// };
// 启用迭代器进行解构
// const [value, setValue] = useState(100);


// const arr = ['a1', 'b1', 'c1'];
// const [a, b, c] = arr;
// console.log(a, b, c);
// console.log({...arr}); // Set Map 无法这样操作

// const likeArr = {};
// const [a2, b2, c2] = likeArr;
// console.log({...likeArr}); 
// console.log(a2, b2, c2);


/**
const arr = [10, 20, 30];
const set = new Set([11, 22, 33]);
const map = new Map([['a', 11], ['b', 22], ['c', 33]]);

const obj = {a: 'aa', b: 'bb', c: 'cc'};
obj[Symbol.iterator] = function() {
    const that = this;
    return {
        keys: Object.keys(that),
        currentVal: 0,
        next() {
            const curKey = this.keys[this.currentVal];
            if(this.currentVal < this.keys.length) {
                this.currentVal++;
                return {done: false, value: that[curKey]};
            } else {
                return {done: true};
            }
        }
    };
}

const obj2 = {0: 10, 1: 20, 2: 30, length: 3};
console.log(Array.from(obj)); // Nodelist

const arr = [10, 20, 30];
for(let n of arr){
    console.log('n', n);
}
const set = new Set([11, 22, 33]);
for(let n2 of set){
    console.log('n2', n2);
}
const obj = {a: 'aa', b: 'bb', c: 'cc'};
obj[Symbol.iterator] = function() {
    const that = this;
    return {
        keys: Object.keys(that),
        currentVal: 0,
        next() {
            const curKey = this.keys[this.currentVal];
            if(this.currentVal < this.keys.length) {
                this.currentVal++;
                return {done: false, value: that[curKey]};
            } else {
                return {done: true};
            }
        }
    };
}
for(let n3 of obj){
    console.log('n3', n3);
}
const obj2 = {
    from: 200,
    to: 202,
};
obj2[Symbol.iterator] = function() {
    this.current = this.from;
    return this;
}
obj2.next = function() {
    if (this.current <= this.to) {
        return {done: false, value: this.current++};
    } else {
        return {done: true};
    }
}
for(let n4 of obj2){
    console.log('n4', n4);
}

const map = new Map([['a', 11], ['b', 22], ['c', 33]]);
for(let n5 of map){
    console.log('n5', n5);
}
console.log(...map);
//*/

// console.log(Array.from(new Set([11, 22, 33])));
// console.log(Array.from(new Map([['a', 11], ['b', 22], ['c', 33]])));

// const obj2 = {
//     from: 200, 
//     to: 202,
// };
// obj2[Symbol.iterator] = function() {
//     this.current = this.from;
//     return this;
// }
// obj2.next = function() {
//     if (this.current <= this.to) {
//         return {done: false, value: this.current++};
//     } else {
//         return {done: true};
//     }
// }
// console.log([...obj2]);
// console.log({...obj2});